package com.ruoyi.know.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.know.domain.CourseChapters;
import com.ruoyi.know.domain.UserLikes;
import com.ruoyi.know.service.IPointRecordsService;
import com.ruoyi.know.service.IUserLikesService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.know.domain.KnowledgePoints;
import com.ruoyi.know.service.IKnowledgePointsService;
import com.ruoyi.common.utils.poi.ExcelUtil;

/**
 * 知识点Controller
 *
 * @author ruoyi
 * @date 2025-01-26
 */
@RestController
@RequestMapping("/know/points")
public class KnowledgePointsController extends BaseController
{
    @Autowired
    private IKnowledgePointsService knowledgePointsService;

    @Autowired
    private IUserLikesService userLikesService;

    @Autowired
    private IPointRecordsService pointRecordsService;

    /**
     * 查询知识点列表
     */
    @PreAuthorize("@ss.hasPermi('know:points:list')")
    @GetMapping("/list")
    public AjaxResult list(KnowledgePoints knowledgePoints,@RequestParam(required = false) String chapterName)
    {
        if (chapterName!=null&& !chapterName.isEmpty()){
            CourseChapters courseChapters = new CourseChapters();
            courseChapters.setChapterName(chapterName);
            knowledgePoints.setCourseChapters(courseChapters);
        }
        List<KnowledgePoints> list = knowledgePointsService.selectKnowledgePointsList(knowledgePoints);
        for (KnowledgePoints knowledgePoints1 : list){
            setKnowledgePoints(knowledgePoints1);
        }
        return success(list);
    }

    private void setKnowledgePoints(KnowledgePoints knowledgePoints){
        //计算点赞数量
        UserLikes userLikes = new UserLikes();
        userLikes.setLikeType(3L);
        userLikes.setInfoId(knowledgePoints.getKnowledgePointId());
        knowledgePoints.setLikesNum(userLikesService.getCountByLikes(userLikes));

        //判断是否已经点过赞
        int countByLikes = userLikesService.getCountByLikes(userLikes);
        knowledgePoints.setLike(countByLikes>0);
    }


    /**
     * 根据章节id查询知识点列表
     */
    @PreAuthorize("@ss.hasPermi('know:points:queryByChapterId')")
    @GetMapping("/queryByChapterId")
    public AjaxResult queryByChapterId(KnowledgePoints knowledgePoints,@RequestParam(required = false) Long chapterId)
    {
        List<KnowledgePoints> list = knowledgePointsService.selectKnowledgePointsListByChapterId(chapterId);
        return success(list);
    }

    /**
     * 导出知识点列表
     */
    @PreAuthorize("@ss.hasPermi('know:points:export')")
    @Log(title = "知识点", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, KnowledgePoints knowledgePoints)
    {
        List<KnowledgePoints> list = knowledgePointsService.selectKnowledgePointsList(knowledgePoints);
        ExcelUtil<KnowledgePoints> util = new ExcelUtil<KnowledgePoints>(KnowledgePoints.class);
        util.exportExcel(response, list, "知识点数据");
    }

    /**
     * 获取知识点详细信息
     */
    @PreAuthorize("@ss.hasPermi('know:points:query')")
    @GetMapping(value = "/{knowledgePointId}")
    public AjaxResult getInfo(@PathVariable("knowledgePointId") Long knowledgePointId)
    {
        KnowledgePoints knowledgePoints = knowledgePointsService.selectKnowledgePointsByKnowledgePointId(knowledgePointId);
        setKnowledgePoints(knowledgePoints);
        int visitQuantity = knowledgePoints.getVisitQuantity();
        knowledgePoints.setVisitQuantity(visitQuantity+1);
        knowledgePointsService.updateKnowledgePoints(knowledgePoints);
        //加积分
        pointRecordsService.earnPoints(getUserId(),"浏览知识点",true);
        return success(knowledgePoints);
    }

    /**
     * 新增知识点
     */
    @PreAuthorize("@ss.hasPermi('know:points:add')")
    @Log(title = "知识点", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody KnowledgePoints knowledgePoints)
    {
        return toAjax(knowledgePointsService.insertKnowledgePoints(knowledgePoints));
    }

    /**
     * 修改知识点
     */
    @PreAuthorize("@ss.hasPermi('know:points:edit')")
    @Log(title = "知识点", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody KnowledgePoints knowledgePoints)
    {
        return toAjax(knowledgePointsService.updateKnowledgePoints(knowledgePoints));
    }

    /**
     * 设置某种附件的url为空
     */
    @PreAuthorize("@ss.hasPermi('know:points:setUrlToNull')")
    @Log(title = "知识点", businessType = BusinessType.UPDATE)
    @PutMapping("/setUrlToNull")
    public AjaxResult setUrlToNull(Integer type,Long knowledgePointId)
    {
        return toAjax(knowledgePointsService.cleanXTypeUrl(type,knowledgePointId));
    }


    /**
     * 删除知识点
     */
    @PreAuthorize("@ss.hasPermi('know:points:remove')")
    @Log(title = "知识点", businessType = BusinessType.DELETE)
	@DeleteMapping("/{knowledgePointIds}")
    public AjaxResult remove(@PathVariable Long[] knowledgePointIds)
    {
        return toAjax(knowledgePointsService.deleteKnowledgePointsByKnowledgePointIds(knowledgePointIds));
    }

    /**
     * 获取前五浏览量的知识点
     * @return
     */
    @GetMapping("/topVisits")
    public AjaxResult getTop5PointsByVisit() {
        return success(knowledgePointsService.selectTop5PointsByVisit());
    }
}
